	var mySwiper1 = new Swiper ('.swiper-container1', {
		direction: 'horizontal',
		loop: false,
		// effect : 'cube',
		// 如果需要分页器
		pagination: '.page',
		paginationClickable: true,
		paginationBulletRender: function (index, className) {
		  return '<div class="' + className + '">' + (index + 1) + '</div>';
		}
	}); 
	var mySwiper2 = new Swiper ('.swiper-container2', {
		direction: 'horizontal',
		loop: false,
		// effect : 'cube',
		// 如果需要分页器
		pagination: '.page',
		paginationClickable: true,
		paginationBulletRender: function (index, className) {
		  return '<div class="' + className + '">' + (index + 1) + '</div>';
		}
	}); 
	var mySwiper3 = new Swiper ('.swiper-container3', {
		direction: 'horizontal',
		loop: false,
		// effect : 'cube',
		// 如果需要分页器
		pagination: '.page',
		paginationClickable: true,
		paginationBulletRender: function (index, className) {
		  return '<div class="' + className + '">' + (index + 1) + '</div>';
		}
	}); 




$(function(){
	var left=$(".total-lead").offset().left;
	$(".addpart").css("width",left+'px');

	var imgwidth=$(".read-part-info").width();
	$(".top-pic img").css("width",imgwidth+1+'px');



	// 每一个块的动态效果
	var $part=$(".swiper-slide>div");
	$part.hover(function(){
		$(this).find("[class=read-part-wrap]").stop(true).animate({"top":"-160px"},300);
	},function(){
		$(this).find("[class=read-part-wrap]").stop(true).animate({"top":"0px"},300);
	});
	$("[class=addpart]").next('.model').hover(function(){
		$(this).find("[class=read-part-wrap]").stop(true).animate({"top":"-160px"},300);
	},function(){
		$(this).find("[class=read-part-wrap]").stop(true).animate({"top":"0px"},300);
	});
	$(".top-do").click(function(){
		location.href=$(this).find('a').attr("href");
	});
	$(".top-info").click(function(){
		location.href=$(this).find('a').attr("href");
	});


	// 左侧的总布局点击效果
	var $part=$(".totalpart");
	var $lead=$(".leadpart");
	var heightmain=$(".main-part").offset().top;
	$lead.click(function(){
		var $this=$(this);
		var index=$lead.index(this);
		var $i=$this.find('i:first-child');

		if ($i.hasClass('fa-chevron-circle-right')) {
			$i.removeClass("fa-chevron-circle-right").addClass("fa-chevron-circle-down");
		}else{
			$i.removeClass("fa-chevron-circle-down").addClass("fa-chevron-circle-right");
		}
		$this.next('ul').toggleClass('hidden');
		for (var i = 0; i < $(".part-lead ul").length; i++) {
			if (i!=index) {
				$(".part-lead>div ul")
				.eq(i).addClass("hidden")
				.parent().find('i:first-child').removeClass("fa-chevron-circle-down").addClass("fa-chevron-circle-right");
			}
		}
		var top=$part.eq(index).offset().top;
		$('html,body').animate({scrollTop:top-heightmain},400);
	});


	// 左侧固定的js
	var $partlead=$("[class=part-lead]");
	$partlead.css("top",80+'px');
	


	// 点击左侧相应发生颜色变化
	var $changeColorTr=$(".total-lead li a");
	$changeColorTr.click(function(){
		var trLength=$changeColorTr.length;
		var thisIndex=$changeColorTr.index(this);
		$(this).addClass("addColor").blur();
		for (var i = 0; i < trLength; i++) {
			if (i!=thisIndex) {
					$changeColorTr.eq(i).removeClass("addColor");
			}
		}
	});


	// 做题
	$(".top-do").click(function(){
		var href=$(this).find('a').attr("href");
		location.href=href;
	})


});


$(window).resize(function() {
	var left=$(".total-lead").offset().left;
	$(".addpart").css("width",left+'px');
});



// 调用ajax封装函数
function getdata(urldata){
	$.ajax({
		url:"?r=ajax/getdata"+urldata,
		dataType:'json',
		type:'get',
		success:function(data){
			var ready=data;
			var allLength=getDataLength(data);   
			var length=allLength-3;
			var objwrap=getObj(data,'class1');
			var $objwrap=$(objwrap);			//获取相应滑动部分的对象
			setTit(data.class1,data.class2_info);


			$objwrap.empty();						//对这个滑动的swiper-slide清空
			$objwrap.siblings().remove();			//对这个主滑动的其余滑动页删除

			var $swiperclone=$objwrap.clone(true);   //对这个滑动页的复制，并清空每一块
			$swiperclone.empty();

			$objwrap.parent().siblings('.page').removeClass('swiper-pagination-clickable').removeClass('swiper-pagination-bullets').empty();			//对滑动的swiper-slide的页数清除

			for(var j = 0;j < length/9; j++){					//如果top>9，则要分页，j为分的页数的总数
				if (j==0){							//每一页的每一个TOP的设置
					for(var x=0;x<9;x++){								//如果j==0，即只有一页，相应的代码
						if(x<length){
							var $clone=$("[class=addpart]").next(".model").clone(true).removeClass("hidden");
							var num=j*9+x;
							switch(objwrap){
								case '#reading1':$clone=setvalr(data,data[num],$clone);break;
								case '#writing1':$clone=setvalw(data,data[num],$clone);break;
								case '#speaking1':$clone=setvals(data,data[num],$clone);break;
							}
							$clone.appendTo($objwrap);
						}
					}
					if((length/9-1)<=0){
						switch(objwrap){
							case '#reading1':mySwiper1.update(); $('.swiper-container1 .page .swiper-pagination-bullet:first-child').click();break;
							case '#writing1':mySwiper2.update(); $('.swiper-container2 .page .swiper-pagination-bullet:first-child').click();break;
							case '#speaking1':mySwiper3.update(); $('.swiper-container3 .page .swiper-pagination-bullet:first-child').click();break;
						}
					}
				}else{							//如果j>0，即不只是第一页
					$swiperclone=$(objwrap).clone(true).empty().removeClass('swiper-slide-active');
					$swiperclone.attr("id",data.class1+(j+1));
					switch(objwrap){
						case '#reading1':$swiperclone.insertAfter(".swiper-container1 .swiper-slide:last-child");break;
						case '#writing1':$swiperclone.insertAfter(".swiper-container2 .swiper-slide:last-child");break;
						case '#speaking1':$swiperclone.insertAfter(".swiper-container3 .swiper-slide:last-child");break;
					}
					for(var x=0;x<9;x++){									
						if(x<(length-j*9)){
							var $clone=$("[class=addpart]").next(".model").clone(true).removeClass("hidden");
							var num=j*9+x;
							switch(objwrap){
								case '#reading1':$clone=setvalr(data,data[num],$clone);break;
								case '#writing1':$clone=setvalw(data,data[num],$clone);break;
								case '#speaking1':$clone=setvals(data,data[num],$clone);break;
							}
							$clone.appendTo($swiperclone);
						}
					}
					if (j>=((length/9)-1)) {
						switch(objwrap){
							case '#reading1':mySwiper1.update(); $('.swiper-container1 .page .swiper-pagination-bullet:first-child').click();break;
							case '#writing1':mySwiper2.update(); $('.swiper-container2 .page .swiper-pagination-bullet:first-child').click();break;
							case '#speaking1':mySwiper3.update(); $('.swiper-container3 .page .swiper-pagination-bullet:first-child').click();break;
						}
					}
				}
			}


			$.ajax({
				url:"?r=ajax/getstate",
				dataType:'json',
                method:'get',
				data:{
					'class1':ready.class1
				},
				success:function(data){
					var arr=setDone(ready.class1,data,ready);
					switch(ready.class1){
						case 'reading':{
							var $allinfo=$(".swiper-container1 .read-part-info");
							for(var i=0; i<arr.length;i++){
								$allinfo.eq(arr[i]).find(".complete").text("已完成");
							}
						};break;
						case 'speaking':{
							var $allinfo=$(".swiper-container3 .read-part-info");
							for(var i=0; i<arr.length;i++){
								$allinfo.eq(arr[i]).find(".complete").text("已完成");
							}
						};break;
						case 'writing':{
							var $allinfo=$(".swiper-container2 .read-part-info");
							for(var i=0; i<arr.length;i++){
								$allinfo.eq(arr[i]).find(".complete").text("已完成");
							}
						};break;
					}
				}
			})
		}
	})
}
//遍历json对象的属性个数
function getDataLength(data){
	var n=0;
	for(var i in data){
		n++;
	}
	return n;
}


function getObj(data,name){
	return '#'+data[name]+'1';
}

//为点击reading出来的对象添加相应的属性
function setvalr(objmain,obj,$tarobj){
	for(var val in obj){				
		switch(val){
		case 'id':{
				var $href=$tarobj.find(".top-do").find('a');
				$href.attr("href",'?r=reading/index&id='+obj[val]);
				var $cum=$tarobj.find(".top-info").find('a');
				$cum.attr("href",'?r=community/reading&id='+obj[val]);
				break;
			}
		case 'title':$tarobj.find('.top-title span:first-child').text(obj[val]);break;
		case 'info':$tarobj.find('.top-title span:last-child').text(obj[val]);break;
		case 'num':$tarobj.find('span[class=doit-times]').text(obj[val]);break;
		case 'diff':$tarobj.find('span[class=nd]').text(getDiff(obj[val]));break;
		case 'class_num':$tarobj.find('[class=top-passage]').html(getTPO(objmain,obj));break;
		case 'img_url':{
				if (obj[val]) {
					$tarobj.find('.top-pic img').attr("src",obj[val]);
				}break;
			}
		default:break;
		}
	}
	return $tarobj;
}

//为点击speaking出来的对象添加相应的属性
function setvals(objmain,obj,$tarobj){
	for(var val in obj){
		switch(val){
		case 'id':{
				var $href=$tarobj.find(".top-do").find('a');
				$href.attr("href",'?r=speaking/index&id='+obj[val]);
				var $cum=$tarobj.find(".top-info").find('a');
				$cum.attr("href",'?r=community/speaking&id='+obj[val]);
				break;
			}
		// case 'title':$tarobj.find('.top-title span:first-child').text(obj[val]);break;
		case 'info':$tarobj.find('.top-title span:last-child').text(obj[val]);break;
		case 'num':$tarobj.find('span[class=doit-times]').text(obj[val]);break;
		case 'diff':$tarobj.find('span[class=nd]').text(obj[val]);break;
		case 'hear_number':$tarobj.find('[class=top-passage]').html(getAno(objmain,obj));break;
		case 'index_img_url':{
				if (obj[val]) {
					$tarobj.find('.top-pic img').attr("src",obj[val]);
				}break;
			}
		default:break;
		}
	}
	$tarobj.find('.top-title span:first-child').text('');
	$tarobj.find(".ndx").text('');
	return $tarobj;
}

//为点击writing出来的对象添加相应的属性
function setvalw(objmain,obj,$tarobj){
	for(var val in obj){				
		switch(val){
		case 'id':{
				var $href=$tarobj.find(".top-do").find('a');
				$href.attr("href",'?r=writing/index&id='+obj[val]);
				var $cum=$tarobj.find(".top-info").find('a');
				$cum.attr("href",'?r=community/writing&id='+obj[val]);
				break;
			}
		case 'title':$tarobj.find('.top-title span:first-child').text(obj[val]);break;
		case 'info':$tarobj.find('.top-title span:last-child').text(obj[val]);break;
		case 'num':$tarobj.find('span[class=doit-times]').text(obj[val]);break;
		case 'diff':$tarobj.find('span[class=nd]').text(obj[val]);break;
		case 'tnum':$tarobj.find('[class=top-passage]').html(getTnum(objmain,obj));break;
		case 'img_url':{
				if (obj[val]) {
					$tarobj.find('.top-pic img').attr("src",obj[val]);
				}break;
			} 
		default:break;
		}
	}
	$tarobj.find('.top-title span:first-child').text('');
	$tarobj.find(".ndx").text('');
	return $tarobj;
}
function getDiff(diff){
	switch(diff+''){
		case '1':return '简单';break;
		case '2':return '适中';break;
		case '3':return '难';break;
	}
}
//TOP 1 - Passage 1
function getTPO(obj,objs){
	return obj.class2.toUpperCase()+'&nbsp;'+objs.class_num+'&nbsp;-&nbsp;'+'Passage'+'&nbsp;'+objs.passage;
}
function getAno(obj,objs){
	return obj.class2.toUpperCase()+'&nbsp;'+objs.hear_number+'&nbsp;-&nbsp;'+objs.question_number;
}
function getTnum(obj,objs){
	return obj.class2.toUpperCase()+'&nbsp;-&nbsp;'+objs.class_num+'&nbsp;-&nbsp;'+objs.class_name;
}
function setTit(tar,val){
	switch(tar+''){
		case 'reading':$(".read-pra span").text(val.toUpperCase()+'');break;
		case 'writing':$(".write-pra span").text(val.toUpperCase()+'');break;
		case 'speaking':$(".speak-pra span").text(val.toUpperCase()+'');break;
	}
}

function setDone(class1,data,ready){
	//先取得所有原有的  id数组
	var alllength=getDataLength(ready);
	var length=alllength-3;
	var all=[];
	for(var i=0 ;i<length ;i++){
		all.push(ready[i].id);
	}
	var ids=[];			//存取所有已完成id的数组
	var hasDown=[];		//  存取 已完成的 数组   按照  该块所呈现的顺序  存储
    var dat=data;
    for (var i=0; i<dat.length;i++){
        ids.push(dat[i].id)
    }
	switch(class1+''){
		case 'reading':{
			all.forEach(function(item,index){
				var n=ids.indexOf(all[index]);
				if (n!=-1) {
					hasDown.push(index);
				}
			})
		};break;
		case 'writing':{
			all.forEach(function(item,index){
				var n=ids.indexOf(all[index]);
				if (n!=-1) {
					hasDown.push(index);
				}
			})
		};break;
		case 'speaking':{
			all.forEach(function(item,index){
				var n=ids.indexOf(all[index]);
				if (n!=-1) {
					hasDown.push(index);
				}
			})
		};break;
	}
	return hasDown;
}
function getThisId($demo){
	var id=$demo.find('.top-do a').attr("href");

}